var express = require('express');
var path = require('path');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();

/* GET users listing. */
router.get('/', function (req, res, next) {
  var db = new sqlite3.Database(path.join(__dirname, "../db/data.s3db"));
  try {
    var info = "";
    db.serialize(function () {
      var no = req.query.no;
      var ip1 = req.query.ip1;
      //sqlStr = "CREATE TABLE IF NOT EXISTS TBL_meta (ID varchar2(255), label varchar2(255))"
      var sqlStr = "update student set uslock=uslock+1 where no='" + no + "'";
      var sqlStr2 = "insert into getlog (stno, ip, ip2, do, gettime2) values ('" + no + "','" + ip1 + "','" + getIp(req) + "', 'lock',datetime(strftime('%s','now') , 'unixepoch', 'localtime'))";

      db.all(sqlStr2,function(err, rows){

      });

      db.serialize(function () {
        db.all(sqlStr, function (err, rows) {
          res.end("锁定成功！");
        });
      })
    });

    //res.send('ok');    
  } catch (ex) {
    console.log(ex);
  }
});
var getIp = function (req) {
  var ip = req.headers['x-real-ip'] || req.headers['x-forwarded-for'] || req.connection.remoteAddres || req.socket.remoteAddress || '';
  if (ip.split(',').length > 0) {
    ip = ip.split(',')[0];
  }
  return ip;
}

module.exports = router;
